package com.soonfor.evaluate.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.othertask.bean.FileBean;
import com.soonfor.evaluate.adapter.PhotoAdapter;
import com.soonfor.evaluate.bean.EvaluateListBean;
import com.soonfor.evaluate.bean.EvaluateListDetailBean;
import com.soonfor.evaluate.base.EvalBaseActivity;
import com.soonfor.evaluate.presenter.Evaluate_IToCustomersDetailPresenter;
import com.soonfor.evaluate.tools.FullyGridLayoutManager;
import com.soonfor.evaluate.view.EvaluateLevlView;

/**
 * 作者：DC-DingYG on 2018-10-19 9:20
 * 邮箱：dingyg012655@126.com
 * 我对客户的评价
 */

public class Evaluate_IToCustomersDetailActivity extends EvalBaseActivity<Evaluate_IToCustomersDetailPresenter> {

    private Activity mActivity;

    @BindView(R.id.llfAllEval)
    LinearLayout llfAllEval;
    @BindView(R.id.tvfAllEval)
    TextView tvfAllEval;
    @BindView(R.id.llfEvalLevl)
    LinearLayout llfEvalLevl;
    @BindView(R.id.viewfEvalLevl)
    EvaluateLevlView viewfEvalLevl;
    @BindView(R.id.llfEvalTime)
    LinearLayout llfEvalTime;
    @BindView(R.id.tvfEvalTime)
    TextView tvfEvalTime;
    @BindView(R.id.llfEvalContent)
    LinearLayout llfEvalContent;
    @BindView(R.id.tvfEvalContent)
    TextView tvfEvalContent;
    @BindView(R.id.rvfIEvalPics)
    RecyclerView rvfIEvalPics;
    private GridLayoutManager manager;
    private PhotoAdapter adapter;
    private EvaluateListBean eBean;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_evaluate_i_to_clients_detail;
    }

    @Override
    protected void initPresenter() {
        presenter = new Evaluate_IToCustomersDetailPresenter(this);
    }

    @Override
    protected void initViews() {
        mActivity = Evaluate_IToCustomersDetailActivity.this;
        mSwipeRefresh.setEnableLoadmore(false);
        eBean = (EvaluateListBean) getIntent().getSerializableExtra("ITOCLIENT_ITEM");
        if (eBean != null) {
            mSwipeRefresh.autoRefresh();
//            if (eBean.getFappraisecststate()==0) {//待评价
//                showNoDataHint("尚未评价, 暂无评价详情");
//            } else if(eBean.getFappraisecststate()==1){//已评价
//                mSwipeRefresh.autoRefresh();
//            } else if(eBean.getFappraisecststate()==3){//不需要评价
//                mSwipeRefresh.setEnableRefresh(false);
//                showNoDataHint("不需要评价");
//            }
        } else {
            finish();
        }
    }

    @Override
    public void RefreshData(boolean isRefresh) {
        super.RefreshData(isRefresh);
        if (eBean!=null) presenter.getDetail(mActivity, eBean.getFappraiseoritype(), eBean.getFappraiseoriid());
    }

    //为界面设数据
    public void showViewByData(final EvaluateListDetailBean detailBean, String msg) {
        if (detailBean != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (detailBean.getFifallhighappraise() == 1) {//开启整体评价
                                llfAllEval.setVisibility(View.VISIBLE);
                                tvfAllEval.setText(detailBean.getFallhighappraiseresultdesc());
                            }else {
                                llfAllEval.setVisibility(View.GONE);
                            }
                            llfEvalTime.setVisibility(View.VISIBLE);
                            tvfEvalTime.setText(detailBean.getFappraisedate());
                            if (detailBean.getFifstarlvappraise() == 1 && detailBean.getAppraisestars().size()>0) {
                                llfEvalLevl.setVisibility(View.VISIBLE);
                                viewfEvalLevl.initEvaluateLevlList(mActivity, detailBean.getAppraisestars());
                            }
                        }
                    });
                }
            }).start();
            final ArrayList<String> evalPics = new ArrayList<>();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (detailBean.getAppraiseimgs().size() > 0) {
                        List<FileBean> attachDtos = detailBean.getAppraiseimgs();
                        for (int i = 0; i < attachDtos.size(); i++) {
                            evalPics.add(attachDtos.get(i).getFpath());
                        }
                    }
                    if (evalPics.size() > 0 && detailBean.getFifuploadimg() == 1) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                manager = new FullyGridLayoutManager(mActivity, 3);
                                rvfIEvalPics.setLayoutManager(manager);
                                adapter = new PhotoAdapter(mActivity, evalPics, PhotoAdapter.TYPE_URL, false);
                                adapter.setListener(new PhotoAdapter.onItemClick() {
                                    @Override
                                    public void itemClick(View view, ArrayList<String> beans, int adapterPosition) {
                                        Bundle bundle1 = new Bundle();
                                        bundle1.putStringArrayList("piclists", evalPics);
                                        bundle1.putInt("position", adapterPosition);
                                        Intent intent = new Intent(mActivity, ShowPicActivity.class);
                                        intent.putExtras(bundle1);
                                        mActivity.startActivity(intent);
                                    }

                                    @Override
                                    public void deleteClick(View view, ArrayList<String> beans, int adapterPosition) {
                                    }
                                });
                                rvfIEvalPics.setAdapter(adapter);
                            }
                        });
                    }
                }
            }).start();
            if (detailBean.getFifsetappraisecontent() == 1) {
                llfEvalContent.setVisibility(View.VISIBLE);
                tvfEvalContent.setText(detailBean.getFappraisecontent());
            }else {
                llfEvalContent.setVisibility(View.GONE);
            }
            showDataToView(null);
        }else {
            if(msg!=null && !msg.equals(""))
                showNoDataHint(msg);
            else
                showNoDataHint("详情为空");
        }
    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }
}
